/*
 * Author: Alberto De Pena
 * Applied Algorithms
 * colls.cpp
*/
#include <iostream>
#include <fstream>
#include <set>
#include <queue>
#include <cstdlib>
using namespace std;

struct Queen
{
    long long x, y;

    bool operator<( const Queen& queen ) const
    {
        if( y > queen.y )
            return true;
        else if( y == queen.y )
        {
            if( x > queen.x )
                return true;
        }
        return false;
    }
};

void main()
{
    ifstream fin;
    ofstream fout;

	fin.open( "collision.in" );
    fout.open( "collision.out" );

    long long c, i, n, g, k, x, y, s, t;
	long long differentx, differenty, colls;
	bool right, down, downR, downL;

    Queen queen;
    set<Queen> Queens;
    set<Queen>::iterator Iter, end;
    priority_queue<Queen> priorityQueens;


    fin >> n;

    while( n > 0 )
    {
        fin >> g;
        colls = 0;

        for( c = 0; c < g; ++c )
        {
            fin >> k >> x >> y >> s >> t;
            for( i = 0; i < k; ++i )
            {
                queen.x = x + i * s;
                queen.y = y + i * t;

                Queens.insert( queen );
                priorityQueens.push( queen );
            }
        }

        while( !priorityQueens.empty() )
        {
            right = down = downR = downL = false;
            
            queen = priorityQueens.top();
            priorityQueens.pop();
            Queens.erase( queen );

            end = Queens.end();
            for( Iter = Queens.begin(); Iter != end; ++Iter )
            {
                if( !right && (*Iter).y == queen.y )
                    right = true;

                if( !down && (*Iter).x == queen.x )
                    down = true;

                if( !downR || !downL )
                {
                    differentx = (*Iter).x - queen.x;
                    differenty = (*Iter).y - queen.y;

                    if( abs( differentx ) == abs( differenty ) )
                    {
                        if( differentx > 0 )
                            downR = true;
                        else
                            downL = true;
                    }
                }
            }

            if( right )
                ++colls;

            if( down )
                ++colls;

            if( downR )
                ++colls;

            if( downL )
                ++colls;
        }

        fout << colls << endl;
        fin >> n;
    }

    fin.close();
    fout.close();
}


